<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="dataForm.status" placeholder="请选择类型">
          <el-option
            label="未激活"
            :value="0"
          >
          </el-option>
          <el-option
            label="已激活"
            :value="1"
          >
          </el-option>
          <el-option
            label="禁用"
            :value="2"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      stripe
      v-loading="dataListLoading"
      style="width: 100%"
    >
      <el-table-column prop="cdk" label="激活码"> </el-table-column>
      <el-table-column prop="starttime" label="创建时间"> </el-table-column>
      <el-table-column prop="stoptime" label="结束时间"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 2" size="small" type="danger">禁用</el-tag>
          <el-tag v-if="scope.row.status === 0" size="small">未激活</el-tag>
          <el-tag v-if="scope.row.status === 1" size="small">已激活</el-tag>
        </template> </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle2(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>

    <!-- 批量新增框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="visible">
      <el-form
        :model="cdkForm"
        :rules="dataRule"
        ref="cdkForm"
        @keyup.enter.native="cdkFormSubmit()"
        label-width="80px"
      >
        <el-form-item label="数量" prop="num">
          <el-input-number
            v-model="cdkForm.num"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="到期时间" prop="stoptime">
          <el-date-picker
            v-model="cdkForm.stoptime"
            type="datetime"
            placeholder="选择到期时间"
            default-time="12:00:00"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button :disabled="saveBtn" type="primary" @click="cdkFormSubmit()"
          >确定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :visible.sync="visible2">
      <el-form
        :model="cdkForm2"
        :rules="dataRule2"
        ref="cdkForm2"
        label-width="80px"
      >
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="cdkForm2.status">
            <el-radio :label="2">禁用</el-radio>
            <el-radio :label="0">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="到期时间" prop="stoptime">
          <el-date-picker
            v-model="cdkForm2.stoptime"
            type="datetime"
            placeholder="选择到期时间"
            default-time="12:00:00"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible2 = false">取消</el-button>
        <el-button type="primary" @click="cdkFormSubmit2()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      saveBtn: false,
      visible: false,
      visible2: false,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataForm: {
        status: null,
      },
      cdkForm: {
        num: 1,
        stoptime: null,
      },
      cdkForm2: {
        id: null,
        status: null,
        stoptime: null,
      },
      dataRule: {
        num: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        stoptime: [
          {
            required: true,
            message: "到期时间不能为空",
            trigger: "blur",
          },
        ],
      },
      dataRule2: {
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        stoptime: [
          {
            required: true,
            message: "到期时间不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  components: {},
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取cdk分页数据
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/sf/activecode/getCdkList"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          status: this.dataForm.status,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data);
          this.dataList = data.cdkPage.records;
          this.totalPage = data.cdkPage.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 表单提交
    cdkFormSubmit() {
      // 表单验证
      this.$refs["cdkForm"].validate((valid) => {
        if (valid) {
          this.$confirm(`如果添加的激活码过多的话，请您耐心等待`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.saveBtn = true;
              this.$http({
                url: this.$http.adornUrl(`/sf/activecode/saveBatchCdk`),
                method: "get",
                params: this.$http.adornParams(this.cdkForm, false),
              }).then(({ data }) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: "操作成功",
                    type: "success",
                    duration: 1500,
                    onClose: () => {
                      this.saveBtn = true;
                      this.visible = false;
                      this.getDataList();
                    },
                  });
                } else {
                  this.$message.error(data.msg);
                }
              });
            })
            .catch(() => {});
        }
      });
    },
    // 表单提交
    cdkFormSubmit2() {
      // 表单验证
      this.$refs["cdkForm2"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/activecode/update`),
            method: "post",
            data: this.$http.adornParams(this.cdkForm2, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible2 = false;
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 新增或修改
    addOrUpdateHandle() {
      // 重置表单
      this.$nextTick(() => {
        this.$refs["cdkForm"].resetFields();
      });
      this.visible = true;
    },
    // 新增或修改
    addOrUpdateHandle2(id) {
      // 重置表单
      this.$nextTick(() => {
        this.$refs["cdkForm2"].resetFields();
      });
      this.cdkForm2.id = null;
      this.$http({
        url: this.$http.adornUrl(`/sf/activecode/info/${id}`),
        method: "get",
      }).then(({ data }) => {
        this.cdkForm2.id = data.activecode.id;
        this.cdkForm2.stoptime = data.activecode.stoptime;
        this.cdkForm2.status = data.activecode.status;
      });
      this.visible2 = true;
    },
    // 删除
    deleteHandle(id) {
      this.$confirm(`确定对[激活码id为${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/activecode/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>